package com.sannu.emc;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sannu.emc.db.DbManager;

/**
 * Servlet implementation class Insertion
 */
public class RegistrationInsertion extends HttpServlet {
	private static final long serialVersionUID = 1L;
	PreparedStatement pst;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegistrationInsertion() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		response.setContentType("text/html");
		PrintWriter pw = response.getWriter();
        
	    String Driver = "com.mysql.jdbc.Driver";
		String connectionURL = "jdbc:mysql://localhost:3306/STUDENTS";
		Connection connection;
        int max=0;
        int Reg_Id=0;
		try {
            
			String name = request.getParameter("name");
			String image = request.getParameter("image");
			String at = request.getParameter("address");
			String district = request.getParameter("district");
			String city = request.getParameter("city");
			String state = request.getParameter("state");
			String zip = request.getParameter("zip");
			String email = request.getParameter("email");
			String mob = request.getParameter("mob");
			String sex = request.getParameter("sex");
			String course = request.getParameter("course");
			String course_fee = request.getParameter("course_fee");
			String paid = request.getParameter("paid");
			
			String checkbox="";
			String lang[]=request.getParameterValues("chk");
			for(int i=0;i<lang.length;i++){
			    checkbox+=lang[i]+" ";
			}
			String username= request.getParameter("user");
			String password= request.getParameter("pass");
			String terms= request.getParameter("terms");
		
			
			Class.forName(Driver);

			connection = DriverManager.getConnection(connectionURL, "root",
					"sannu");

			pst = connection
					.prepareStatement("insert into REGISTRATION (name, image, at, district, city, state, zip, email, mob, sex, course, course_fee, paid, checkbox, terms) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
			          
			pst.setString(1, name);
			pst.setString(2, image);
			pst.setString(3, at);
			pst.setString(4, district);
			pst.setString(5, city);
			pst.setString(6, state);
			pst.setString(7, zip);
			pst.setString(8, email);
			pst.setString(9, mob);
			pst.setString(10, sex);
			pst.setString(11, course);
			pst.setString(12, course_fee);
			pst.setString(13, paid);
			pst.setString(14, checkbox);
			pst.setString(15, terms);
			int i = pst.executeUpdate();
			if (i != 0) {
				pw.println("<br>Record has been inserted");

			} else {
				pw.println("failed to insert the data");
			}
			pst=connection.prepareStatement("SELECT MAX(Reg_Id) FROM REGISTRATION");  
			
	          ResultSet rs = pst.executeQuery();
	          
	         if (rs.next())
	         {
	             max= rs.getInt(1);
	           
	         }
	         
	          
	        Reg_Id= max;
	       
			pst= connection.prepareStatement("INSERT INTO LOGIN (username,password,Reg_Id)VALUES(?,?,?)");
			
			pst.setString(1, username);
			pst.setString(2, password);
			pst.setInt(3, Reg_Id);
			int j = pst.executeUpdate();
			if (j != 0) {
				pw.println("<br>Record has been inserted in Login Table");

			} else {
				pw.println("failed to insert the data");
			}
		} catch (Exception e) {
			System.out.println("Exception : " + e.toString());

		}
		
		DbManager dbm = new DbManager();
		dbm.insert(pst);
	}

}
